/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bio.jpa.session;

import com.bio.jpa.entities.*;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.bio.jsf.SessionController;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Diego
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName = "BioDeskPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }

    public List<Session> sessionInfo(int uid) {
        Query q2 = getEntityManager().createNamedQuery("Session.findByUidFk");
        q2.setParameter("uidFk", uid);
        List<Session> s = q2.getResultList();

        if (s != null && s.size() > 1) {
            return s;
        }

        return null;
    }

    public boolean logout(Session entity) {
        getEntityManager().merge(entity);
        return true;
    }

    public User auth(String username, String password) {
        Query q = getEntityManager().createNamedQuery("User.Auth");
        q.setParameter("userLogin", username);
        q.setParameter("userPass", password);
        List<User> u = q.getResultList();
        if (u != null && u.size() > 0) {
            User logged = u.get(0);
            logged = find(logged.getUserId());
            SessionController sc = new SessionController();
            Session ss = new Session(0, logged.getUserId());
            sc.setCurrent(ss);
            getEntityManager().persist(ss);
            return logged;
        } else {
            return null;
        }

    }

    public List<User> findAllLessMe(Integer id) {
        Query q = getEntityManager().createNamedQuery("User.findAllLessMe");
        q.setParameter("userId", id);
        List<User> u = q.getResultList();
        return u;
    }

    public List<User> findByUserLive(Integer flag) {
        Query q = getEntityManager().createNamedQuery("User.findByUserLive");
        q.setParameter("userLive", flag);
        List<User> u = q.getResultList();
        return u;
    }

    public boolean canCreateLogin(String login) {
        Query q = getEntityManager().createNamedQuery("User.findByUserLogin");
        q.setParameter("userLogin", login);
        List<User> u = q.getResultList();
        if (u.size() > 0) {
            return false;
        } else {
            return true;
        }
    }

    public List<UserGroup> findGroupsInotIn(Integer id) {
        Query q = getEntityManager().createNamedQuery("UserGroup.findGroupsInotIn");
        q.setParameter("userId", id);
        List<UserGroup> u = q.getResultList();
        return u;
    }

    public List<Project> findAssignedProjects(Integer id) {
        Query q = getEntityManager().createNamedQuery("User.findAssignedProjects");
        q.setParameter("userId", id);
        List<Project> u = q.getResultList();
        return u;
    }

    public List<Project> findMyProjects(Integer id) {
        Query q = getEntityManager().createNamedQuery("User.findMyProjects");
        q.setParameter("userId", id);
        List<Project> u = q.getResultList();
        return u;
    }

    public List<UserGroup> findMyGroups(Integer id) {
        Query q = getEntityManager().createNamedQuery("UserGroup.findMyGroups");
        q.setParameter("userId", id);
        List<UserGroup> u = q.getResultList();
        return u;
    }
}
